<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>冰泉冷涩弦凝绝，凝绝不通声暂歇</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta property="og:type" content="website">
<meta property="og:title" content="冰泉冷涩弦凝绝，凝绝不通声暂歇">
<meta property="og:url" content="https://gitee.com/xinhard/index.html">
<meta property="og:site_name" content="冰泉冷涩弦凝绝，凝绝不通声暂歇">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="lxp">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/xinhard/atom.xml" title="冰泉冷涩弦凝绝，凝绝不通声暂歇" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/xinhard/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/xinhard/css/style.css">

  
    
<link rel="stylesheet" href="/xinhard/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/xinhard/" id="logo">冰泉冷涩弦凝绝，凝绝不通声暂歇</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/xinhard/">Home</a>
        
          <a class="main-nav-link" href="/xinhard/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/xinhard/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/xinhard"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main">
  
    <article id="post-STM32可调电源" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/xinhard/2023/05/14/STM32%E5%8F%AF%E8%B0%83%E7%94%B5%E6%BA%90/" class="article-date">
  <time class="dt-published" datetime="2023-05-14T15:55:52.000Z" itemprop="datePublished">2023-05-14</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/xinhard/2023/05/14/STM32%E5%8F%AF%E8%B0%83%E7%94%B5%E6%BA%90/">STM32可调电源</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h2 id="项目概括"><a href="#项目概括" class="headerlink" title="项目概括"></a>项目概括</h2><blockquote>
<p>使用PD诱骗器，作为电路总电源，降压模块配合单片机进行调压，OLED屏幕显示电压及其电流、功率值。<br>输出max：20v 3A<br>功能：</p>
<ol>
<li>ADC电压检测 （可调电位器旋钮调节电压）</li>
<li>电流检测</li>
<li>OLED屏幕显示数据（IIC通信）</li>
<li>散热片+风扇（12V）</li>
<li>温度检测</li>
<li>PD电源输入，20V档位</li>
<li>风扇、底板都使用铜柱固定</li>
<li>输入部分可以使用DC接口、TypeC<br><img src="https://img-blog.csdnimg.cn/82920a93df77462bba11c2216210f974.png" alt="在这里插入图片描述"><br>来张实物照片：<br><img src="https://img-blog.csdnimg.cn/539b66ca0a74482bb23e1eb9cf84281b.png" alt="在这里插入图片描述"></li>
</ol>
</blockquote>
<h2 id="硬件模块方案"><a href="#硬件模块方案" class="headerlink" title="硬件模块方案"></a>硬件模块方案</h2><h3 id="PD诱骗电源部分"><a href="#PD诱骗电源部分" class="headerlink" title="PD诱骗电源部分"></a>PD诱骗电源部分</h3><p><img src="https://img-blog.csdnimg.cn/6311e6c864b44f48b85c224dabc7a4e8.png" alt="在这里插入图片描述"></p>
<blockquote>
<p>PD诱骗电源部分使用CH224K芯片，CFG1端的Rest请求电阻可以请求PD快充各个档位电压，9&#x2F;12&#x2F;20V,R4为电压检测引脚，R1电阻通过CH224K设置内部LDO 芯片VDD引脚3.3V电压，C1构成滤波电路。测量实际电路中CH224K工作电流大概30mA，之前使用0603封装电阻发热挺明显的，后来第二个版本换成了1206封装电阻发热明显降低了很多。<br>芯片具体参数和请求电压请参考手册</p>
</blockquote>
<h3 id="电路电源部分"><a href="#电路电源部分" class="headerlink" title="电路电源部分"></a>电路电源部分</h3><p><img src="https://img-blog.csdnimg.cn/6e0cd9d7d9c64b439df2819d5f8f2986.png" alt="在这里插入图片描述"></p>
<blockquote>
<p>上面电路12V输出给风供电，3.3V输出给单片机、电流检测芯片、屏幕供电。</p>
</blockquote>
<h3 id="电压电流功率检测部分"><a href="#电压电流功率检测部分" class="headerlink" title="电压电流功率检测部分"></a>电压电流功率检测部分</h3><blockquote>
<p>INA226通过IIC协议进行电压、电流和功率测量<br>使用INA226 芯片，IIC通信，可以同时检测电压、电流<br>供电：2.7<del>5.5V<br>检测电压范围 0</del>36V<br>VIN检测电路输出端电压<br><img src="https://img-blog.csdnimg.cn/18830cf07284444ab4fd530d65c49a0e.png" alt="在这里插入图片描述"></p>
</blockquote>
<p><strong>计算过程：</strong></p>
<p><img src="https://img-blog.csdnimg.cn/4ffab339a13b49cab620dfa4b1ff0450.png" alt="在这里插入图片描述"><br>公式1为校准寄存器的值，Current_LSB为电流分辨率，Rshunt为采样电阻。<br>Maximum Expected Current 为最大期待电流，可以通过自己的分辨率去算这个值，但不能超过实际的最大电流值。</p>
<p>因为采样电阻为2mΩ，Shunt Voltage Register的最大值为0x7FFF, LSB &#x3D; 2.5uV（固定值）, 所以FSR &#x3D; 81.92mV。<br>所以最大采样电流为0.08192V &#x2F; 0.002Ω &#x3D; 40.96A</p>
<p>假如分辨率为0.001A，则Maximum Expected Current &#x3D; 32768 x 0.001 &#x3D; 32.768A（满足条件）<br>得CAL（校准寄存器）值为 0.00512&#x2F;(0.001*0.002)  &#x3D; 0.00512&#x2F;0.000002 &#x3D; 2560 &#x3D; 0x0A00<br>最后写入Calibration Register 中数据为0x0A00。<br>电流数据通过IIC读取电流寄存器的值乘于电流分辨率即可得到真实的电流值。<br>详情参考手册15页校准寄存器章节。</p>
<h3 id="调压部分"><a href="#调压部分" class="headerlink" title="调压部分"></a>调压部分</h3><blockquote>
<p>LM2596<br><a target="_blank" rel="noopener" href="https://www.amobbs.com/thread-5696234-1-1.html">0V解决方案</a></p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/bea16b0e331047eabfaab8ebbb607749.png" alt="在这里插入图片描述"></p>
<blockquote>
<p>LM2596 开关电压调节器是降压型电源管理单片集成电路，能够输出 3A 的驱动电流，同时具有很好的线性和负载调节特性。固定输出版本有 3.3V、5V、12V， 可调版本可以输出小于 37V 的各种电压。<br>该器件内部集成频率补偿和固定频率发生器，开关频率为 150KHz，与低频开关调节器相比较，可以使用更小规格的滤波元件。由于该器件只需 4 个外接元件，可以使用通用的标准电感，这更优化了LM2596 的使用，极大地简化了开关电源电路的设计。<br>&#x3D;&#x3D;特点:&#x3D;&#x3D;<br>※ 3.3V、5V、12V 的固定电压输出和可调电压输出<br>※ 可调输出电压范围 1.2V～37V±4%<br>※ 输出线性好且负载可调节<br>※ 输出电流可高达 3A<br>※ 输入电压可高达 40V<br>※ 采用 150KHz 的内部振荡频率，属于第二代开关电压调节器，功耗小、效率高<br>※ 低功耗待机模式，IQ的典型值为 80μA<br>※ TTL 断电能力<br>※ 具有过热保护和限流保护功能<br>※ 封装形式：TO-220（T）和 TO-263（S）<br>※ 外围电路简单，仅需 4 个外接元件， 且使用容易购买的标准电感<br><strong>计算过程：</strong><br>计算公式：VOUT &#x3D; 1.23*(1+R3_L&#x2F;R3_R)</p>
</blockquote>
<h3 id="风扇控制电路"><a href="#风扇控制电路" class="headerlink" title="风扇控制电路"></a>风扇控制电路</h3><p><img src="https://img-blog.csdnimg.cn/80651337f7e1421a8c45bbe5125dea87.png" alt="在这里插入图片描述"></p>
<blockquote>
<p>风扇控制电路如上图所示，通过N沟道MOS管控制风扇通断</p>
</blockquote>
<h3 id="测温部分"><a href="#测温部分" class="headerlink" title="测温部分"></a>测温部分</h3><p><img src="https://img-blog.csdnimg.cn/a0e341656c6b47a1887b49131433c474.png" alt="在这里插入图片描述"></p>
<blockquote>
<p>NTC测温，10K</p>
</blockquote>
<p><a target="_blank" rel="noopener" href="https://oshwhub.com/xinhard/stm32-ke-diao-dian-yuan-v2">项目链接：</a></p>
<h3 id="第一个版本和第二个版本记录解决的问题："><a href="#第一个版本和第二个版本记录解决的问题：" class="headerlink" title="第一个版本和第二个版本记录解决的问题："></a>第一个版本和第二个版本记录解决的问题：</h3><blockquote>
<ol>
<li>PD芯片12V档位不正常</li>
<li>PD芯片底部GND接线问题</li>
<li>&#x3D;&#x3D;调节电压的电阻&#x3D;&#x3D;部分需要重新走线！！！</li>
<li>LM2596的二极管一定要方向焊接</li>
<li>INA226的IIC接口和OLED接到一起！！！弃用之前的PB3和PB4引脚</li>
<li>LDO更换大功率，大封装的芯片</li>
<li>CH224K芯片供电滤波电路，1K电阻使用0805封装</li>
<li>Type-C接口换！ 换成容易焊接的封装</li>
<li>电源输入端接3A保险丝</li>
<li>为了方便使用5MM电源座供电，尽量不使用PD快充（成本高），CH224K供电由LDO供电</li>
<li>INA226A的IIC接口，焊盘换成过孔</li>
</ol>
</blockquote>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/xinhard/2023/05/14/STM32%E5%8F%AF%E8%B0%83%E7%94%B5%E6%BA%90/" data-id="clhnlozvx00007ku8gbhn8z22" data-title="STM32可调电源" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
</article>



  
    <article id="post-IIC协议学习" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/" class="article-date">
  <time class="dt-published" datetime="2023-01-28T07:06:02.000Z" itemprop="datePublished">2023-01-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/">IIC协议学习</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <h1 id="IIC协议学习"><a href="#IIC协议学习" class="headerlink" title="IIC协议学习"></a>IIC协议学习</h1><p>&amp;nbsp;&amp;nbsp;IIC (IIC ,Inter – Integrated Circuit )，两线式串行总线，由PHILPS公司开发用于连接微控制器的外围设备。<br>&amp;nbsp;&amp;nbsp;由数据线SDA和时钟线SCL构成的串行总线，可发送和接收数据。在CPU与被控IC之间、IC与IC之间进行双向传送，高速IIC总线一般可达400kbps以上。<br>IIC是半双工通信方式。</p>
<h2 id="IIC协议描述"><a href="#IIC协议描述" class="headerlink" title="IIC协议描述"></a>IIC协议描述</h2><p>多主机IIC总线体系结构<br><img src="https://img-blog.csdnimg.cn/105663c48cf54734b2a92cfa184ddb8f.png" alt="在这里插入图片描述"></p>
<h3 id="数据的有效性"><a href="#数据的有效性" class="headerlink" title="数据的有效性"></a>数据的有效性</h3><blockquote>
<p>&amp;nbsp;&amp;nbsp;IIC 总线进行数据传送时，时钟信号为高电平期间，数据线上数据必须保持稳定，只有在时钟线上的信号为低电平期间，数据线上的高电平或低电平才允许变化。<br>总结：数据在SCL的上升沿到来之前就需要准备好，并在下降沿到来之前必须保持稳定。<br><img src="https://img-blog.csdnimg.cn/24a289e591a04490ac121d0405767dae.png" alt="在这里插入图片描述"><br>&#x3D;&#x3D;一句话&#x3D;&#x3D;概括：在SCL为高时，SDA的状态为有效</p>
</blockquote>
<h3 id="数据传输"><a href="#数据传输" class="headerlink" title="数据传输"></a>数据传输</h3><blockquote>
<p>&amp;nbsp;&amp;nbsp;一般情况下，一个标准IIC通信由4个部分组成：起始信号、从机地址传输、数据传输、停止信号。IIC通信由主机发送一个停止起始信号来启动，然后主机对从机寻址并决定数据传输方向（读or写）。IIC总线上传输数据的最小单位是1个字节（8bit），首先发送数据位为最高位，依次传输完1个字节，接收器必须发送一个应答位。每次通信的数据字节数没有限制的，全部数据传输完毕后，由主机发送停止信号，结束通信。<br><img src="https://img-blog.csdnimg.cn/0a1c19cca5274a14bf0c7ed13462f05d.png" alt="在这里插入图片描述"></p>
</blockquote>
<h3 id="IIC-总线寻址约定"><a href="#IIC-总线寻址约定" class="headerlink" title="IIC 总线寻址约定"></a>IIC 总线寻址约定</h3><blockquote>
<p>规定起始信号后的第1个字节为寻址字节，用来寻址被控器件，并规定数据的传输方向。<br>7位寻址模式：由从机7位地址(D7~D0)和一位读写为（D0）组成。<br>读写位 &#x3D; 1时，表示读，下一个字节开始主机从从机读取数据； 主机为接收器<br>读写位 &#x3D; 0时，表示写，下一个字节开始主机将数据传送给从机；主机为发送器<br>过程： 主机发送起始信号后立即传送寻址字节（广播），总线上所有器件都将寻址字节中的7位地址与自己的地址比较，if 两者相同，则该器件被认为被主机寻址，并发送应答信号<br>寻址字节中的读写位决定了主机和从机是发送器还是接收器。<br>注：不允许有两个地址相同的器件！！！ </p>
</blockquote>
<h3 id="IIC协议内容"><a href="#IIC协议内容" class="headerlink" title="IIC协议内容"></a>IIC协议内容</h3><h4 id="1-空闲状态"><a href="#1-空闲状态" class="headerlink" title="1. 空闲状态"></a>1. 空闲状态</h4><blockquote>
<p>&amp;nbsp;&amp;nbsp;IIC 总线的SDA和SCL两条信号线同时处于高电平时，规定总线为空闲状态。此时，各个器件的输出级场效应管均处于截止状态，即释放总线，由两条信号线上的各自的上拉电阻把电平拉高。</p>
</blockquote>
<h4 id="2-开始信号与停止信号"><a href="#2-开始信号与停止信号" class="headerlink" title="2. 开始信号与停止信号"></a>2. 开始信号与停止信号</h4><blockquote>
<p>&amp;nbsp;&amp;nbsp;起始信号：当SCL为高电平期间，SDA由高到低的跳变；启动信号是一种电平跳变时序信号，而不是一个电平信号。<br>停止信号： 当SCL为高期间，SDA由低电平到高电平的跳变；停止信号也是一种电平跳变的时序信号，而不是一个电平信号。<br><img src="https://img-blog.csdnimg.cn/ade0ace5c3eb4902ba140dae14fdc231.png" alt="在这里插入图片描述"></p>
</blockquote>
<h5 id="开始信号实现："><a href="#开始信号实现：" class="headerlink" title="开始信号实现："></a>开始信号实现：</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 开始信号</span></span><br><span class="line"><span class="comment"> * @param: None</span></span><br><span class="line"><span class="comment"> * @return: void</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Start</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    SDA_OUT();</span><br><span class="line">    SCL = <span class="number">1</span>;</span><br><span class="line">    SDA = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">4</span>);</span><br><span class="line">    SDA = <span class="number">0</span>;</span><br><span class="line">    delay_us(<span class="number">4</span>);</span><br><span class="line">    <span class="comment">// 拉低SCL,准备发送或者接收数据</span></span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>逻辑分析仪抓取的波形：<br><img src="https://img-blog.csdnimg.cn/3316166dd6274a98945a7dcb05fb70a6.png" alt="在这里插入图片描述"></p>
<h5 id="结束信号实现："><a href="#结束信号实现：" class="headerlink" title="结束信号实现："></a>结束信号实现：</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 停止信号</span></span><br><span class="line"><span class="comment"> * @param: None</span></span><br><span class="line"><span class="comment"> * @return: void</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Stop</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    SDA_OUT();</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">    SDA = <span class="number">0</span>;</span><br><span class="line">    delay_us(<span class="number">4</span>);</span><br><span class="line">    SCL = <span class="number">1</span>;</span><br><span class="line">    SDA = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">4</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>逻辑分析仪抓取的波形：<br><img src="https://img-blog.csdnimg.cn/e7663ef20d3549a792d749bd8cd38250.png" alt="在这里插入图片描述"></p>
<h4 id="3-应答信号（ACK）与非应答信号（NACK）"><a href="#3-应答信号（ACK）与非应答信号（NACK）" class="headerlink" title="3. 应答信号（ACK）与非应答信号（NACK）"></a>3. 应答信号（ACK）与非应答信号（NACK）</h4><blockquote>
<p>&amp;nbsp;&amp;nbsp;IIC 总线协议规定，发送器每发送1个字节(8bit)数据，接收器必须产生1个应答信号或者非应答信号。<br>实现方法是：发送器发送完8位数据后，第9个时钟信号将数据线置高电平，接收器根据通信状态可以将数据线拉低，产生一个应答信号（表示接收器成功收到该字节），或者保持数据线为高电平，产生一个非应答信号（表示接收器接收该字节没有成功）</p>
<blockquote>
<p>注：对于反馈有效应答位的要求： 接收器在第9个时钟脉冲之前的低电平期间将SDA线拉低，并确保在该时钟的高电平期间为稳定的低电平。</p>
</blockquote>
<p><img src="https://img-blog.csdnimg.cn/d8f491fda48641a1bc0762e08b49fd38.png" alt="在这里插入图片描述"></p>
</blockquote>
<h5 id="产生应答"><a href="#产生应答" class="headerlink" title="产生应答"></a>产生应答</h5><p>SCL时钟为高时，SDA时钟为低</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 产生应答信号,sda为0</span></span><br><span class="line"><span class="comment"> * @param: None</span></span><br><span class="line"><span class="comment"> * @return: void</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_ACK</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">    SDA_OUT();</span><br><span class="line">    SDA = <span class="number">0</span>;</span><br><span class="line">    delay_us(<span class="number">2</span>);</span><br><span class="line">    SCL = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">2</span>);</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/68cccc2209ae43cba3ddb33cfeaf18ab.png" alt="在这里插入图片描述"></p>
<h5 id="产生非应答"><a href="#产生非应答" class="headerlink" title="产生非应答"></a>产生非应答</h5><p>时钟为高时，SDA为高</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 不产生应答信号,sda为1</span></span><br><span class="line"><span class="comment"> * @param: None</span></span><br><span class="line"><span class="comment"> * @return: void</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_NAck</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">    SDA_OUT();</span><br><span class="line">    SDA = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">2</span>);</span><br><span class="line">    SCL = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">2</span>);</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/fe80904a870f4feb9f547b2e39c09d87.png" alt="在这里插入图片描述"></p>
<h5 id="等待应答"><a href="#等待应答" class="headerlink" title="等待应答"></a>等待应答</h5><p><img src="https://img-blog.csdnimg.cn/f684b49423fb40359cf7adc227b8cfd0.png" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: 等待应答信号（接收）</span></span><br><span class="line"><span class="comment"> * @param: None</span></span><br><span class="line"><span class="comment"> * @return: 0: success , 1: failure</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">u8 <span class="title function_">IIC_Wait_ACK</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">    u8 err_time = <span class="number">0</span>;</span><br><span class="line">    SDA_IN();</span><br><span class="line">    SDA = <span class="number">1</span>;</span><br><span class="line">    SCL = <span class="number">1</span>;</span><br><span class="line">    delay_us(<span class="number">2</span>);</span><br><span class="line">    <span class="keyword">while</span> (Read_SDA)</span><br><span class="line">    &#123;</span><br><span class="line">        err_time++;</span><br><span class="line">        <span class="keyword">if</span> (err_time &gt; <span class="number">255</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            IIC_Stop();</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    SCL = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="4-发送一个字节"><a href="#4-发送一个字节" class="headerlink" title="4. 发送一个字节"></a>4. 发送一个字节</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">IIC_Send(<span class="number">0xA0</span>); <span class="comment">//写</span></span><br></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/933b2bcb5ad64c35bcbbc6d9f0b14deb.png" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 发送一个字节</span></span><br><span class="line"><span class="comment"> * @param: u8 data: 发送的数据</span></span><br><span class="line"><span class="comment"> * @param: 最后一位0:写入 1:读</span></span><br><span class="line"><span class="comment"> *            ref:  WRITE or READ</span></span><br><span class="line"><span class="comment"> * @return: void</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">IIC_Send</span><span class="params">(u8 data)</span></span><br><span class="line">&#123;</span><br><span class="line">    u8 i;</span><br><span class="line">    SDA_OUT();</span><br><span class="line">    SCL = <span class="number">0</span>; <span class="comment">// 拉低时钟开始传输</span></span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">// 从高位开始依次发送</span></span><br><span class="line">        SDA = (data &amp; <span class="number">0x80</span>) &gt;&gt; <span class="number">7</span>;</span><br><span class="line">        <span class="comment">// 移掉最高位</span></span><br><span class="line">        data = data &lt;&lt; <span class="number">1</span>;</span><br><span class="line">        delay_us(<span class="number">2</span>);</span><br><span class="line">        <span class="comment">// 以下部分为产生一个时钟周期</span></span><br><span class="line">        &#123;</span><br><span class="line">            SCL = <span class="number">1</span>;</span><br><span class="line">            delay_us(<span class="number">2</span>);</span><br><span class="line">            SCL = <span class="number">0</span>;</span><br><span class="line">            delay_us(<span class="number">2</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="5-接收一个字节"><a href="#5-接收一个字节" class="headerlink" title="5. 接收一个字节"></a>5. 接收一个字节</h4><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: IIC 接收一个字节</span></span><br><span class="line"><span class="comment"> * @param: u8 ack, 1:发送应答位，0:发送nack</span></span><br><span class="line"><span class="comment"> * @return: u8 temp: 接受到的数据</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">u8 <span class="title function_">IIC_ReceiveData</span><span class="params">(u8 ack)</span></span><br><span class="line">&#123;</span><br><span class="line">    u8 receive = <span class="number">0</span>, i;</span><br><span class="line">    SDA_IN();</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">8</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        SCL = <span class="number">0</span>;</span><br><span class="line">        delay_us(<span class="number">2</span>);</span><br><span class="line">        SCL = <span class="number">1</span>;</span><br><span class="line">        delay_us(<span class="number">2</span>);</span><br><span class="line">        receive = receive &lt;&lt; <span class="number">1</span>;</span><br><span class="line">        <span class="keyword">if</span> (Read_SDA)</span><br><span class="line">            receive++;</span><br><span class="line">        delay_us(<span class="number">2</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (ack)</span><br><span class="line">        IIC_ACK();</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        IIC_NAck();</span><br><span class="line">    <span class="keyword">return</span> receive;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="IIC实战"><a href="#IIC实战" class="headerlink" title="IIC实战"></a>IIC实战</h3><h4 id="AT24C02简介"><a href="#AT24C02简介" class="headerlink" title="AT24C02简介"></a>AT24C02简介</h4><blockquote>
<p>&amp;nbsp;&amp;nbsp;24C02是一个2K bit 的串行EEPROM（掉电不丢失）内部含有256个字节、在24C02里面有个一8字节的页写缓冲器。256 * 8 &#x3D; 2048&#x2F;2K bit容量，&#x3D;&#x3D;总结&#x3D;&#x3D;：每个地址8位，且每个地址能够存储1b数据，有256个地址，也就是能够存256B数据。</p>
<ol>
<li>A0，A1，A2：硬件地址引脚（可以自定义地址）</li>
<li>WP：写保护引脚，接高电平只读，接地允许读和写</li>
<li>SCL和SDA：IIC总线<blockquote>
<p>SCL串行时钟：AT24C02串行时钟输入管脚用于产生器件所有数据发送或者接收时钟，这是个输入管脚。<br>SDA双向串行数据&#x2F;地址管脚，用于器件所有数据的发送或者接收，SDA是一个&#x3D;&#x3D;开漏输出&#x3D;&#x3D;管脚，可与其它开漏输出或者&#x3D;&#x3D;集电极开漏输出&#x3D;&#x3D;进行<strong>线或（wire-OR）</strong></p>
</blockquote>
</li>
<li>高位固定0xA0，可编程3位，最后一位读写位（&#x3D;&#x3D;高（1）为读，低（0）为写&#x3D;&#x3D;）<blockquote>
<p><img src="https://img-blog.csdnimg.cn/6960505290534f9fb9886ca8d9c3ddf2.png" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/2c9d921ad4cb4589b7adf5e73a81a82e.png" alt="在这里插入图片描述"></p>
</blockquote>
</li>
</ol>
</blockquote>
<h5 id="写数据时序"><a href="#写数据时序" class="headerlink" title="写数据时序"></a>写数据时序</h5><blockquote>
<p>先看时序图：</p>
<blockquote>
<p><img src="https://img-blog.csdnimg.cn/8b46d6c194414493ae5efbcdb396130f.png" alt="在这里插入图片描述"><br>操作时序： 启动总线—-发送设备地址+写—-等待应答—-发送数据存储地址（0x00 ~ 0xFF，256个字节）—- 等待应答—- 发送数据—-等待应答—-停止总线。</p>
</blockquote>
<p>&amp;nbsp;&amp;nbsp;在写数据的过程中，每成功写入一个字节，EEPROM存储空间的地址就会自动加1(相当于文件指针)，当加到0xFF后，写一个字节，地址就会溢出又变成0xFF。<br>注意：EEPROM是先写到缓冲区，然后再搬运到“掉电非易失区”。所以这个过程需要一定的时间，AT24C02这个过程不超过5ms，如果这个时候让它去响应是没有相应的！所以，我们在写入一个字节后，再写入1下一个字节前，必须等待EEPROM再次响应即可。</p>
</blockquote>
<h5 id="读数据时序"><a href="#读数据时序" class="headerlink" title="读数据时序"></a>读数据时序</h5><blockquote>
<p>读操作与写操作初始化相同，只是器件地址中的读&#x2F;写位应选择为1，有三种不同的读操作方式：</p>
<blockquote>
<p> 当前地址读 、随机读、顺序读</p>
</blockquote>
</blockquote>
<ol>
<li>当前地址读<br>&amp;nbsp;&amp;nbsp;内部地址计数器保存着上一次访问时最后一个地址加1的值，只要芯片有电，改地址就一直保存。当读到最后页的最后字节，地址会回转到0；当写到某页尾的最后一个字节，地址会回转到该页的首字节。<br><img src="https://img-blog.csdnimg.cn/e2a646191a4345759338b906c9603680.png" alt="在这里插入图片描述"></li>
<li>随机读<br>&amp;nbsp;&amp;nbsp;随机读需要先写一个目标字地址，一旦EEPROM接收器器件地址和直堤子并应答了ACK，主器件就产生一个重复的起始条件。然后，主器件发送器件地址（读写选择位为1），EEPROM应答ACK，并随时钟送出数据。主器件无需应答0，但是需发送停止条件。<br><img src="https://img-blog.csdnimg.cn/5ca119ce67bf4d5caffc310e658094a1.png" alt="在这里插入图片描述"></li>
<li>顺序读<br>&amp;nbsp;&amp;nbsp;顺序读可以通过当前地址读或随机读启动。主器件接收到一个数据后，应答ACK。只要EEPROM接收到ACK，将自动增加字地址并继续随时钟发送后面的数据。若达到存储地址末尾，地址自动回转到0，仍可继续顺序读取数据。<br>  <img src="https://img-blog.csdnimg.cn/588a984ec7c44130bfd114ebef8d431a.png" alt="在这里插入图片描述"><blockquote>
<p>具体的器件特性请参考 《24C02手册》<br><a target="_blank" rel="noopener" href="https://item.szlcsc.com/3428355.html">AT24C02手册</a></p>
</blockquote>
</li>
</ol>
<h4 id="24C02写入一个字符"><a href="#24C02写入一个字符" class="headerlink" title="24C02写入一个字符"></a>24C02写入一个字符</h4><p>逻辑分析仪抓取的波形：<br>以下波形功能：<br>在器件0xA0,的0x00地址上写入字符E的ASCII码<br><img src="https://img-blog.csdnimg.cn/b9b4e9b762a04351a9b4ea78d0f1b9dd.png" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: 指定地址写入一个字节数据</span></span><br><span class="line"><span class="comment"> * @param: u8 address:</span></span><br><span class="line"><span class="comment"> * @param: u8 data</span></span><br><span class="line"><span class="comment"> * @return: None</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">AT24C02_WriteByAddressOfBytes</span><span class="params">(u8 address, u8 data)</span></span><br><span class="line">&#123;</span><br><span class="line">    IIC_Start();</span><br><span class="line">    <span class="comment">// 器件地址+写</span></span><br><span class="line">    IIC_Send(<span class="number">0xA0</span>);</span><br><span class="line">    IIC_Wait_ACK();</span><br><span class="line">    <span class="comment">// 发送要写入的数据地址</span></span><br><span class="line">    IIC_Send(address);</span><br><span class="line">    IIC_Wait_ACK();</span><br><span class="line">    <span class="comment">// 写入数据</span></span><br><span class="line">    IIC_Send(data);</span><br><span class="line">    IIC_ACK();</span><br><span class="line">    IIC_Stop();</span><br><span class="line">    <span class="comment">// 24c02先把数据放在缓冲区里，再写入掉电非易失区</span></span><br><span class="line">    delay_ms(<span class="number">10</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h4 id="24C02读取一个字符"><a href="#24C02读取一个字符" class="headerlink" title="24C02读取一个字符"></a>24C02读取一个字符</h4><p>  使用逻辑分析仪读取的数据：<br>  地址0x01之前写入0x88数据，现在读取0x01地址上的数据，结果为0x88，正确(✿◡‿◡)<br>  <img src="https://img-blog.csdnimg.cn/9325219c978246bf8809550a2cb0e146.png" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (<span class="number">1</span>)</span><br><span class="line">&#123;</span><br><span class="line">	key_value = KEY_Scan(<span class="number">0</span>); <span class="comment">// 按键处理程序</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span> (key_value == KEY1_PRES)<span class="comment">// 写入</span></span><br><span class="line">	&#123;</span><br><span class="line">		AT24C02_WriteByAddressOfBytes(<span class="number">1</span>,<span class="number">0x88</span>);</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span> (key_value == KEY0_PRES)<span class="comment">// 读取</span></span><br><span class="line">	&#123;</span><br><span class="line">		from_24c02_data = AT24C02_ReadOneByteOfAddress(<span class="number">1</span>);</span><br><span class="line">		<span class="comment">//printf(&quot;from 24c02 read data:%d\n&quot;,from_24c02_data);</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>  <img src="https://img-blog.csdnimg.cn/0af9a1bc290e4ef59154c076e3accfd4.png" alt="在这里插入图片描述"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: 指定地址读取一个字节的数据</span></span><br><span class="line"><span class="comment"> * @param: u8 address:</span></span><br><span class="line"><span class="comment"> * @return: u8 data</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">u8 <span class="title function_">AT24C02_ReadOneByteOfAddress</span><span class="params">(u8 address)</span></span><br><span class="line">&#123;</span><br><span class="line">    u8 temp;</span><br><span class="line">    IIC_Start();</span><br><span class="line">    <span class="comment">// 器件地址+写</span></span><br><span class="line">    IIC_Send(<span class="number">0xA0</span>);</span><br><span class="line">    IIC_Wait_ACK();</span><br><span class="line">    <span class="comment">// 数据地址</span></span><br><span class="line">    IIC_Send(address);</span><br><span class="line">    IIC_Wait_ACK();</span><br><span class="line">	IIC_Start();</span><br><span class="line">	<span class="comment">// 器件地址+读</span></span><br><span class="line">    IIC_Send(<span class="number">0xA0</span>|READ);</span><br><span class="line">    IIC_ACK();</span><br><span class="line">    <span class="comment">//接收数据，并发送非应答位</span></span><br><span class="line">    temp = IIC_ReceiveData(<span class="number">0</span>);</span><br><span class="line">    IIC_Stop();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> temp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: 指定地址读取length数据</span></span><br><span class="line"><span class="comment"> * @param: u8 read_start</span></span><br><span class="line"><span class="comment"> * @param: u8 length</span></span><br><span class="line"><span class="comment"> * @param: u8 *buffer</span></span><br><span class="line"><span class="comment"> * @return None</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">AT24C02_ReadOneBytesOfLength</span><span class="params">(u8 read_start, u8 *buffer, u8 length)</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (length--)</span><br><span class="line">    &#123;</span><br><span class="line">        *buffer++ = AT24C02_ReadOneByteOfAddress(read_start++);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * @brief: 指定地址写入指定长度的数据</span></span><br><span class="line"><span class="comment"> * @param: u8 start_address</span></span><br><span class="line"><span class="comment"> * @param: u8 *buffer</span></span><br><span class="line"><span class="comment"> * @param: u8 length</span></span><br><span class="line"><span class="comment"> * @return: None</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span> <span class="title function_">AT24C02_WriteByAddressOfLengthData</span><span class="params">(u8 start_address, u8 *buffer, u8 length)</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">while</span> (length--)</span><br><span class="line">    &#123;</span><br><span class="line">        AT24C02_WriteByAddressOfBytes(start_address++, *buffer);</span><br><span class="line">        buffer++;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>&#x3D;&#x3D;总结&#x3D;&#x3D;：</p>
<blockquote>
<p>IIC功能非常强大，这里使用软件IIC是为了方便移植程序。我们在使用中只需用关注具体器件IIC的时序即可，写好时序，通过逻辑分析仪去Debug分析程序代码，进而实现功能。</p>
</blockquote>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/" data-id="clhnlozw400017ku8e7hidlz8" data-title="IIC协议学习" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
</article>



  
    <article id="post-物联网毕设-智能鱼缸" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/" class="article-date">
  <time class="dt-published" datetime="2022-09-25T07:54:02.000Z" itemprop="datePublished">2022-09-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/">物联网毕设-智能鱼缸</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <blockquote>
<p>主要记录项目开发过程遇到的bug，项目进度、以及各个功能模块的实现</p>
</blockquote>
<p>@<a href="%E7%89%A9%E8%81%94%E7%BD%91%E9%B1%BC%E7%BC%B8%E6%AF%95%E8%AE%BE">TOC</a></p>
<hr>
<h1 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h1><p><code>关键字：物联网、网页与硬件交互、传感器数据上传、配网</code></p>
<p>智能鱼缸实现Wifi联网、网页后台控制、传感器数据上传、基本的鱼缸DIY搭建</p>
<hr>
<p><code>提示：以下是本篇文章正文内容，下面案例可供参考</code></p>
<h1 id="一、主要硬件模块"><a href="#一、主要硬件模块" class="headerlink" title="一、主要硬件模块"></a>一、主要硬件模块</h1><h2 id="1、原理图："><a href="#1、原理图：" class="headerlink" title="1、原理图："></a>1、原理图：</h2><h3 id="1-、温度传感器（18b20）"><a href="#1-、温度传感器（18b20）" class="headerlink" title="(1)、温度传感器（18b20）"></a>(1)、温度传感器（18b20）</h3><p>   <img src="https://img-blog.csdnimg.cn/562fb2f395124a59a2ff006681ca7353.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_17,color_FFFFFF,t_70,g_se,x_16" alt="温度传感器18b20"></p>
<h3 id="2-、继电器"><a href="#2-、继电器" class="headerlink" title="(2)、继电器"></a>(2)、继电器</h3><p>原理图<br><img src="https://img-blog.csdnimg.cn/55ea9131f27f4224a43efb515548d847.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br><img src="https://img-blog.csdnimg.cn/410153c7ad494e15a659db962e3a4782.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h3 id="3-、NODE-MCU-、8266模块及自动下载电路、串口通信"><a href="#3-、NODE-MCU-、8266模块及自动下载电路、串口通信" class="headerlink" title="(3)、NODE MCU 、8266模块及自动下载电路、串口通信"></a>(3)、NODE MCU 、8266模块及自动下载电路、串口通信</h3><p>8266模块<br><img src="https://img-blog.csdnimg.cn/4688592183c04c3bbac25fdc241e18c1.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>自动下载电路<br><img src="https://img-blog.csdnimg.cn/d5f9b4535f38436090d51331a3565f6c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_13,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>使用usb接口供电兼串口下载&#x2F;串口通信<br><img src="https://img-blog.csdnimg.cn/2dc4a00e62c945ea8b6c1c92b977658c.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_19,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>板载外围的电路：<br><img src="https://img-blog.csdnimg.cn/fb8a47921f0f4df8be1af3e11b5a8b58.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"><br>其它版本的NodeMCU板子  PCB图、和本次使用100%兼容<br><img src="https://img-blog.csdnimg.cn/5994104be0e3466b9caf6ddb0eca7795.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAeGluaGFyZA==,size_20,color_FFFFFF,t_70,g_se,x_16" alt="在这里插入图片描述"></p>
<h3 id="部分驱动代码："><a href="#部分驱动代码：" class="headerlink" title="部分驱动代码："></a>部分驱动代码：</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;Arduino.h&gt;</span></span></span><br><span class="line"><span class="comment">//单总线18b20驱动库</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;OneWire.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;DallasTemperature.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">define</span> ONE_WIRE_BUS 13<span class="comment">//8266连接GPIO13即D7，Arduino连接port 2</span></span></span><br><span class="line"><span class="comment">// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)</span></span><br><span class="line"><span class="function">OneWire <span class="title">oneWire</span><span class="params">(ONE_WIRE_BUS)</span></span>;</span><br><span class="line"> </span><br><span class="line"><span class="comment">// Pass our oneWire reference to Dallas Temperature. </span></span><br><span class="line"><span class="function">DallasTemperature <span class="title">sensors</span><span class="params">(&amp;oneWire)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">setup</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  Serial.<span class="built_in">begin</span>(<span class="number">9600</span>);</span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;Dallas Temperature IC Control Library Demo&quot;</span>);</span><br><span class="line">  <span class="comment">// Start up the library</span></span><br><span class="line">  sensors.<span class="built_in">begin</span>();</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">loop</span><span class="params">(<span class="type">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123; </span><br><span class="line">  <span class="comment">// call sensors.requestTemperatures() to issue a global temperature </span></span><br><span class="line">  <span class="comment">// request to all devices on the bus</span></span><br><span class="line">  Serial.<span class="built_in">print</span>(<span class="string">&quot;Requesting temperatures...&quot;</span>);</span><br><span class="line">  sensors.<span class="built_in">requestTemperatures</span>(); <span class="comment">// Send the command to get temperatures</span></span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;DONE&quot;</span>);</span><br><span class="line">  <span class="comment">// After we got the temperatures, we can print them here.</span></span><br><span class="line">  <span class="comment">// We use the function ByIndex, and as an example get the temperature from the first sensor only.</span></span><br><span class="line">  <span class="type">float</span> tempC = sensors.<span class="built_in">getTempCByIndex</span>(<span class="number">0</span>);</span><br><span class="line">  <span class="comment">// Check if reading was successful</span></span><br><span class="line">  <span class="keyword">if</span>(tempC != DEVICE_DISCONNECTED_C) </span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;Temperature for the device 1 (index 0) is: &quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(tempC);</span><br><span class="line">  &#125; </span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Error: Could not read temperature data&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="效果"><a href="#效果" class="headerlink" title="效果"></a>效果</h3><p><img src="https://img-blog.csdnimg.cn/28d2c4c99ee941abb3c949bf9b582055.png" alt="测温"></p>
<h3 id="与网页交换数据"><a href="#与网页交换数据" class="headerlink" title="与网页交换数据"></a>与网页交换数据</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;wd&quot;</span>) <span class="comment">//温度</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">//更新temptrue数据</span></span><br><span class="line">  <span class="built_in">read_temperature</span>();</span><br><span class="line">  esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(temperature));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="网页端请求数据"><a href="#网页端请求数据" class="headerlink" title="网页端请求数据"></a>网页端请求数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">wendu</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">var</span> xmlhttp;</span><br><span class="line">    <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">        xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">    &#125;</span><br><span class="line">    xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">            <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;wd&quot;</span>).<span class="property">innerHTML</span> = xmlhttp.<span class="property">responseText</span> + <span class="string">&quot;℃&quot;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;wd&quot;</span>);</span><br><span class="line">    xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="2、主要外围设备连接"><a href="#2、主要外围设备连接" class="headerlink" title="2、主要外围设备连接"></a>2、主要外围设备连接</h2><p>因为使用的都是模块，所以就懒得画原理图、弄封装了。这里直接记录8266端口与外围设备的接线：</p>
<table>
<thead>
<tr>
<th>NodeMcu端口</th>
<th>对应模块接线</th>
</tr>
</thead>
<tbody><tr>
<td>D0</td>
<td>喂食继电器</td>
</tr>
<tr>
<td>D1</td>
<td>抽水电机继电器</td>
</tr>
<tr>
<td>D2</td>
<td>增氧继电器</td>
</tr>
<tr>
<td>D5</td>
<td>0.96 OLED SCL</td>
</tr>
<tr>
<td>D6</td>
<td>0.96 OLED SDA</td>
</tr>
<tr>
<td>D7（8266的GPIO13）</td>
<td>18B20数据端 （5.1K上拉电阻）</td>
</tr>
<tr>
<td>D8</td>
<td>灯光控制</td>
</tr>
</tbody></table>
<p>继电器使用5v电源，光耦隔离，高电平有效。OLED使用3.3V供电。由于IO口不够使用，使用D0引脚，D0是MCU睡眠唤醒引脚，8266上电时默认会有一个跳变信号，所以继电器会动作一下，8266本来实际有效引脚就很少。</p>
<h3 id="供电"><a href="#供电" class="headerlink" title="供电"></a>供电</h3><pre><code>推荐使用USB手机插头或者充电宝供电，不推荐使用8266板载micro usb供电
</code></pre>
<h1 id="二、网页端"><a href="#二、网页端" class="headerlink" title="二、网页端"></a>二、网页端</h1><h2 id="1-WIFI配网"><a href="#1-WIFI配网" class="headerlink" title="1.WIFI配网"></a>1.WIFI配网</h2><p>主要实现步骤：ESP8266先运行在AP模式，手机或者电脑连接热点后，弹出强制门户网站（WiFi配网页面），点击网页上配网按钮，即可扫描周围全部WIFI热点，点击热点名称即可把信息输入SSID文本框，用户只需用输入密码即可。最后点击连接WIFI按钮，ESP8266收到网页的SSID、Password信息后，连接此WIFI，如果连接成功，自动跳转到主页面否则重新刷新该页面，网页上显示密码错误。</p>
<h3 id="写入网页文件地址"><a href="#写入网页文件地址" class="headerlink" title="写入网页文件地址"></a>写入网页文件地址</h3><p>使plantform io上传<br><img src="https://img-blog.csdnimg.cn/ac7e33a816e94f41923e88496d887416.png" alt="在这里插入图片描述"></p>
<h3 id="WIFI-配置部分后台代码"><a href="#WIFI-配置部分后台代码" class="headerlink" title="WIFI 配置部分后台代码"></a>WIFI 配置部分后台代码</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">else</span></span><br><span class="line"> &#123;                          <span class="comment">//未配置过WIFI</span></span><br><span class="line">   <span class="keyword">if</span> (wifiStatus == <span class="literal">false</span>) <span class="comment">//未连接</span></span><br><span class="line">   &#123;</span><br><span class="line">     <span class="comment">// digitalWrite(2, LOW);</span></span><br><span class="line">     WiFi_Status = <span class="string">&quot;0&quot;</span>;</span><br><span class="line">     EEPROM.<span class="built_in">write</span>(WiFi_State_Addr, <span class="number">0</span>);</span><br><span class="line">     EEPROM.<span class="built_in">commit</span>();</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">disconnect</span>(<span class="literal">true</span>); <span class="comment">//断开连接</span></span><br><span class="line">     Serial.<span class="built_in">println</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">print</span>(<span class="string">&quot;Start WiFi config \n softAP starting --&gt; &quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">println</span>(AP_name);</span><br><span class="line">     Serial.<span class="built_in">print</span>(<span class="string">&quot;http://&quot;</span>);</span><br><span class="line">     Serial.<span class="built_in">println</span>(url);</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">mode</span>(WIFI_AP_STA); <span class="comment">//配置为热点模式</span></span><br><span class="line">     WiFi.<span class="built_in">softAPConfig</span>(apIP, apIP, <span class="built_in">IPAddress</span>(<span class="number">255</span>, <span class="number">255</span>, <span class="number">255</span>, <span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">     WiFi.<span class="built_in">softAP</span>(AP_name); <span class="comment">//热点地址</span></span><br><span class="line">   &#125;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="扫描WIFI-并处理成json"><a href="#扫描WIFI-并处理成json" class="headerlink" title="扫描WIFI 并处理成json"></a>扫描WIFI 并处理成json</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//获取WiFi加密类型并返回</span></span><br><span class="line"><span class="function">String <span class="title">wifi_type</span><span class="params">(<span class="type">int</span> typecode)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_NONE)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;Open&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_WEP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WEP &quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_TKIP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA &quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_CCMP)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA2&quot;</span>;</span><br><span class="line">  <span class="keyword">if</span> (typecode == ENC_TYPE_AUTO)</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;WPA*&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="string">&quot;NONE&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//扫描WiFi，返回json</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">wifiScan</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  String req_json = <span class="string">&quot;&quot;</span>;</span><br><span class="line">  Serial.<span class="built_in">println</span>(<span class="string">&quot;Scan WiFi&quot;</span>);</span><br><span class="line">  <span class="type">int</span> n = WiFi.<span class="built_in">scanNetworks</span>();</span><br><span class="line">  <span class="keyword">if</span> (n &gt; <span class="number">0</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="type">int</span> m = <span class="number">0</span>;</span><br><span class="line">    req_json = <span class="string">&quot;&#123;\&quot;req\&quot;:[&quot;</span>;</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; n; i++)</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">if</span> ((<span class="type">int</span>)WiFi.<span class="built_in">RSSI</span>(i) &gt;= Signal_filtering)</span><br><span class="line">      &#123;</span><br><span class="line">        m++;</span><br><span class="line">        req_json += <span class="string">&quot;&#123;\&quot;ssid\&quot;:\&quot;&quot;</span> + (String)WiFi.<span class="built_in">SSID</span>(i) + <span class="string">&quot;\&quot;,&quot;</span> + <span class="string">&quot;\&quot;encryptionType\&quot;:\&quot;&quot;</span> + <span class="built_in">wifi_type</span>(WiFi.<span class="built_in">encryptionType</span>(i)) + <span class="string">&quot;\&quot;,&quot;</span> + <span class="string">&quot;\&quot;rssi\&quot;:&quot;</span> + (<span class="type">int</span>)WiFi.<span class="built_in">RSSI</span>(i) + <span class="string">&quot;&#125;,&quot;</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    req_json.<span class="built_in">remove</span>(req_json.<span class="built_in">length</span>() - <span class="number">1</span>);</span><br><span class="line">    req_json += <span class="string">&quot;]&#125;&quot;</span>;</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/json;charset=UTF-8&quot;</span>, req_json);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot;Found &quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">print</span>(m);</span><br><span class="line">    Serial.<span class="built_in">print</span>(<span class="string">&quot; WiFi!  &gt;&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">print</span>(Signal_filtering);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;dB&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;scan all wifi :&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(req_json);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="WIFI配置处理部分"><a href="#WIFI配置处理部分" class="headerlink" title="WIFI配置处理部分"></a>WIFI配置处理部分</h3><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//用于WiFi配置，接收ssid、password两个参数</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">wifiConfig</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="comment">// 判断请求是否存在这两个参数以及是否未配置</span></span><br><span class="line">  <span class="keyword">if</span> (esp8266_server.<span class="built_in">hasArg</span>(<span class="string">&quot;ssid&quot;</span>) &amp;&amp; esp8266_server.<span class="built_in">hasArg</span>(<span class="string">&quot;password&quot;</span>) &amp;&amp; WiFi_Status == <span class="string">&quot;0&quot;</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="type">int</span> ssid_len = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;ssid&quot;</span>).<span class="built_in">length</span>();</span><br><span class="line">    <span class="type">int</span> password_len = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;password&quot;</span>).<span class="built_in">length</span>();</span><br><span class="line">    <span class="comment">// 判断ssid和密码长度是否合理</span></span><br><span class="line">    <span class="keyword">if</span> ((ssid_len &gt; <span class="number">0</span>) &amp;&amp; (ssid_len &lt; <span class="number">33</span>) &amp;&amp; (password_len &gt; <span class="number">7</span>) &amp;&amp; (password_len &lt; <span class="number">65</span>))</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="comment">// 把参数获得的String赋值到ssid</span></span><br><span class="line">      String ssid_str = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;ssid&quot;</span>);</span><br><span class="line">      String password_str = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;password&quot;</span>);</span><br><span class="line">      <span class="type">const</span> <span class="type">char</span> *ssid = ssid_str.<span class="built_in">c_str</span>();</span><br><span class="line">      <span class="type">const</span> <span class="type">char</span> *password = password_str.<span class="built_in">c_str</span>();</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;SSID: &quot;</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(ssid);</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;Password: &quot;</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(password);</span><br><span class="line">      <span class="comment">// 开始连接WiFi</span></span><br><span class="line">      WiFi.<span class="built_in">begin</span>(ssid, password);</span><br><span class="line">      Serial.<span class="built_in">print</span>(<span class="string">&quot;Connenting&quot;</span>);</span><br><span class="line">      <span class="comment">//判断是否连接成功，8秒超时</span></span><br><span class="line">      <span class="type">unsigned</span> <span class="type">long</span> millis_time = <span class="built_in">millis</span>();</span><br><span class="line">      <span class="keyword">while</span> ((WiFi.<span class="built_in">status</span>() != WL_CONNECTED) &amp;&amp; (<span class="built_in">millis</span>() - millis_time &lt; <span class="number">8000</span>))</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">500</span>);</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;.&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 判断是否连接成功</span></span><br><span class="line">      <span class="keyword">if</span> (WiFi.<span class="built_in">status</span>() == WL_CONNECTED)</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="comment">// 连接成功，返回1</span></span><br><span class="line">        <span class="built_in">digitalWrite</span>(<span class="number">2</span>, HIGH);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;Connected successfully!&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;IP Address: &quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(WiFi.<span class="built_in">localIP</span>());</span><br><span class="line">        Serial.<span class="built_in">print</span>(<span class="string">&quot;http://&quot;</span>);</span><br><span class="line">        Serial.<span class="built_in">println</span>(Hostname);</span><br><span class="line">        esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;1&quot;</span>);</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">300</span>);</span><br><span class="line">        WiFi_Status = <span class="string">&quot;1&quot;</span>;</span><br><span class="line">        <span class="comment">// 记录状态到EEPROM：已配置</span></span><br><span class="line">        EEPROM.<span class="built_in">write</span>(WiFi_State_Addr, <span class="string">&#x27;1&#x27;</span>);</span><br><span class="line">        EEPROM.<span class="built_in">commit</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//存储WIFI SSID、Password到EEPROM中</span></span><br><span class="line">        <span class="built_in">writeString</span>(<span class="number">10</span>, ssid_str, password_str);</span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;save ok&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">50</span>);</span><br><span class="line">        <span class="comment">//关闭热点</span></span><br><span class="line">        WiFi.<span class="built_in">softAPdisconnect</span>();</span><br><span class="line"></span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">2000</span>);</span><br><span class="line">        ESP.<span class="built_in">restart</span>();</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">else</span></span><br><span class="line">      &#123;</span><br><span class="line">        esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;0&quot;</span>);</span><br><span class="line">        <span class="comment">// 连接失败, 返回0</span></span><br><span class="line">        Serial.<span class="built_in">println</span>(<span class="string">&quot;Connenting failed!&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">    &#123;</span><br><span class="line">      <span class="comment">// WiFi密码格式错误</span></span><br><span class="line">      Serial.<span class="built_in">println</span>(<span class="string">&quot;Password format error&quot;</span>);</span><br><span class="line">      esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;Password format error&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 请求参数错误</span></span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;Request parameter error&quot;</span>);</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;Request parameter error&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="WIFI-配置部分网页端"><a href="#WIFI-配置部分网页端" class="headerlink" title="WIFI 配置部分网页端"></a>WIFI 配置部分网页端</h3><p>网页端使用html显示，点击扫描按钮，发送处理请求到ESP8266端，ESP8266调用函数，处理成json数据返回给html，使用<strong>Ajax</strong>接收数据，接收完毕后会显示所有WIFI列表，用户点击某个WIFI名称后输入密码，此时<strong>Ajax</strong>返回数据给ESP8266，8266开始调用连接WIFI函数，无论是否连接成功都会返回给网页端数据给html页面上，给用户显示，成功后8266会重启。</p>
<h3 id="存储WIFI-SSID、PassWord到EEPROM中"><a href="#存储WIFI-SSID、PassWord到EEPROM中" class="headerlink" title="存储WIFI SSID、PassWord到EEPROM中"></a>存储WIFI SSID、PassWord到EEPROM中</h3><p>这两个普通操作函数我写了大几个小时、可能那天状态不是很好，各方面测试都试过、包括中文也可以存储到EEPROM中。</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*@brief：把SSID 、Password写入EEPROM中</span></span><br><span class="line"><span class="comment">*地址8：ssid_str 长度 地址9：pws_str长度，</span></span><br><span class="line"><span class="comment">*从地址10开始为ssid_str、pws_str连续存储内容</span></span><br><span class="line"><span class="comment">*@return: psw_start 密码地址起始位置</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="type">uint8_t</span> <span class="title">writeString</span><span class="params">(<span class="type">int</span> start,String ssid_str,String psw)</span></span>&#123;</span><br><span class="line">  </span><br><span class="line">      <span class="comment">//String ssid_str = &quot;micro&quot;;</span></span><br><span class="line">      <span class="comment">//String psw = &quot;arduino01&quot;;</span></span><br><span class="line"></span><br><span class="line">      <span class="type">uint8_t</span> ssid_str_len = ssid_str.<span class="built_in">length</span>();</span><br><span class="line">      <span class="type">uint8_t</span> psw_len = psw.<span class="built_in">length</span>();</span><br><span class="line">      EEPROM.<span class="built_in">write</span>(<span class="number">8</span>,ssid_str_len);</span><br><span class="line">      EEPROM.<span class="built_in">write</span>(<span class="number">9</span>,psw_len);</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      <span class="comment">//写入ssid、psw字符串</span></span><br><span class="line">      <span class="type">int</span> ssid_start_addre;</span><br><span class="line">      <span class="type">int</span> ssid_end_addre;</span><br><span class="line">      <span class="type">int</span> psw_start;</span><br><span class="line">      <span class="type">int</span> psw_end_addre;</span><br><span class="line">      <span class="comment">//读取ssid的长度、psw的长度</span></span><br><span class="line">      <span class="comment">//uint8_t ssid_len = EEPROM.read(8);</span></span><br><span class="line">      <span class="comment">//uint8_t psw_len = EEPROM.read(9);</span></span><br><span class="line">      <span class="comment">//写入ssid_str      </span></span><br><span class="line">      <span class="type">int</span> i;</span><br><span class="line">      <span class="keyword">for</span>(i = start;i &lt; start+ssid_str_len;i++)&#123;</span><br><span class="line">          EEPROM.<span class="built_in">write</span>(i,ssid_str[i-start]);</span><br><span class="line">          <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      ssid_end_addre = i;</span><br><span class="line">      psw_start = i+<span class="number">1</span>;</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      <span class="comment">//写入psw，在ssid_str结束的下一个位置</span></span><br><span class="line">      <span class="keyword">for</span>(i = psw_start;i &lt; psw_start+psw_len; i++)&#123;</span><br><span class="line">        EEPROM.<span class="built_in">write</span>(i,psw[i-psw_start]);</span><br><span class="line">        <span class="built_in">delay</span>(<span class="number">30</span>);</span><br><span class="line">      &#125;</span><br><span class="line">      EEPROM.<span class="built_in">commit</span>();</span><br><span class="line">      psw_end_addre = i;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> psw_start;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">*@brief:从地址10开始读取SSID、PAssword</span></span><br><span class="line"><span class="comment">*@Param：uint8_t： flag:0 返回ssid_str  1:返回psw_str</span></span><br><span class="line"><span class="comment">*@return String</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function">String <span class="title">getStringInfo</span><span class="params">(<span class="type">uint8_t</span> flag)</span></span>&#123;</span><br><span class="line">    <span class="comment">//从EEprom中读ssid_str</span></span><br><span class="line">      String temp_str = <span class="string">&quot;&quot;</span>;</span><br><span class="line">      <span class="type">int</span> i,psw_start;</span><br><span class="line">      <span class="type">uint8_t</span> ssid_str_len,pws_str_len;</span><br><span class="line"></span><br><span class="line">      ssid_str_len = EEPROM.<span class="built_in">read</span>(<span class="number">8</span>);</span><br><span class="line">      pws_str_len = EEPROM.<span class="built_in">read</span>(<span class="number">9</span>);</span><br><span class="line">      Serial.<span class="built_in">println</span>(ssid_str_len);</span><br><span class="line">      Serial.<span class="built_in">println</span>(pws_str_len);</span><br><span class="line">      <span class="keyword">if</span> (flag == <span class="number">0</span>) <span class="comment">//ssid_str</span></span><br><span class="line">      &#123;</span><br><span class="line">           <span class="keyword">for</span>(i = <span class="number">10</span>;i &lt; <span class="number">10</span>+ssid_str_len;i++)&#123;</span><br><span class="line">                temp_str += <span class="built_in">char</span>(EEPROM.<span class="built_in">read</span>(i));</span><br><span class="line">           &#125;</span><br><span class="line">           </span><br><span class="line">      &#125;<span class="keyword">else</span> <span class="keyword">if</span>(flag == <span class="number">1</span>)&#123;    <span class="comment">//psw_str</span></span><br><span class="line">            psw_start = <span class="number">10</span>+ssid_str_len+<span class="number">1</span>;</span><br><span class="line">            <span class="keyword">for</span>(i = psw_start;i &lt; pws_str_len+psw_start;i++)&#123;</span><br><span class="line">                temp_str += <span class="built_in">char</span>(EEPROM.<span class="built_in">read</span>(i));</span><br><span class="line">           &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">return</span> temp_str;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="2-网页数据显示、控制"><a href="#2-网页数据显示、控制" class="headerlink" title="2.网页数据显示、控制"></a>2.网页数据显示、控制</h2><p>代码如下（示例）：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//处理index所有数据获取请求</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">sjhq</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   String parm = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;sjhq&quot;</span>);</span><br><span class="line">   <span class="keyword">if</span> (parm == <span class="string">&quot;wd&quot;</span>) <span class="comment">//温度</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;respond wen du request.&quot;</span>);</span><br><span class="line">    <span class="comment">//更新temptrue数据</span></span><br><span class="line">    <span class="built_in">read_temperature</span>();</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(temperature));</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;light&quot;</span>) <span class="comment">//灯光按钮</span></span><br><span class="line">  &#123;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;light clicked...&quot;</span>);</span><br><span class="line">    dg = !dg;</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;light:&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(dg?<span class="string">&quot;on&quot;</span>:<span class="string">&quot;off&quot;</span>);</span><br><span class="line"></span><br><span class="line">   <span class="built_in">digitalWrite</span>(Light, dg);</span><br><span class="line"></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> <span class="keyword">if</span> (parm == <span class="string">&quot;sxhs&quot;</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    sxh = !sxh;</span><br><span class="line">    <span class="built_in">digitalWrite</span>(ShuiXunHuan, sxh);</span><br><span class="line"></span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;水循环&quot;</span>);</span><br><span class="line"></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;zy&quot;</span>)&#123;   <span class="comment">//增氧</span></span><br><span class="line">    zeng_yang = !zeng_yang;</span><br><span class="line"></span><br><span class="line">    <span class="built_in">digitalWrite</span>(ZengYang,zeng_yang);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;ZengYang:&quot;</span>+zeng_yang);</span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>,<span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line"></span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;zengyangStatus&quot;</span>)&#123; <span class="comment">//获取增氧开关状态</span></span><br><span class="line">      Serial.<span class="built_in">println</span>(<span class="string">&quot;respond zengyangStatus request.&quot;</span>);</span><br><span class="line">      esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>, <span class="string">&quot;text/plain&quot;</span>, <span class="built_in">String</span>(<span class="built_in">digitalRead</span>(ZengYang)?<span class="number">1</span>:<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">  &#125;<span class="keyword">else</span> <span class="keyword">if</span>(parm == <span class="string">&quot;timeFeed&quot;</span>)&#123;</span><br><span class="line"></span><br><span class="line">    String value = esp8266_server.<span class="built_in">arg</span>(<span class="string">&quot;value&quot;</span>);</span><br><span class="line">    Serial.<span class="built_in">println</span>(<span class="string">&quot;select timer value:&quot;</span>+value);</span><br><span class="line">    <span class="comment">//电机转3s后停止</span></span><br><span class="line">    <span class="comment">//digitalWrite(ZengYang,HIGH);</span></span><br><span class="line">    <span class="built_in">digitalWrite</span>(Feed,HIGH);</span><br><span class="line">    <span class="built_in">delay</span>(<span class="number">3000</span>);</span><br><span class="line">    <span class="built_in">digitalWrite</span>(Feed,LOW);</span><br><span class="line">    <span class="comment">//digitalWrite(ZengYang,LOW);</span></span><br><span class="line">    esp8266_server.<span class="built_in">send</span>(<span class="number">200</span>,<span class="string">&quot;text/plain&quot;</span>, <span class="string">&quot;ok&quot;</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3、网页端数据请求"><a href="#3、网页端数据请求" class="headerlink" title="3、网页端数据请求"></a>3、网页端数据请求</h2><p>下面只展示两个处理、其它大同小异</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">wendu</span>(<span class="params"></span>) &#123;</span><br><span class="line">       <span class="keyword">var</span> xmlhttp;</span><br><span class="line">       <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">           xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">               <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;wd&quot;</span>).<span class="property">innerHTML</span> = xmlhttp.<span class="property">responseText</span> + <span class="string">&quot;℃&quot;</span>;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;wd&quot;</span>);</span><br><span class="line">       xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">   &#125;</span><br><span class="line">       <span class="keyword">function</span> <span class="title function_">shuixunhuan</span>(<span class="params"></span>) &#123;</span><br><span class="line">       <span class="keyword">var</span> xmlhttp;</span><br><span class="line">       <span class="keyword">if</span> (<span class="variable language_">window</span>.<span class="property">XMLHttpRequest</span>) &#123;</span><br><span class="line">           xmlhttp = <span class="keyword">new</span> <span class="title class_">XMLHttpRequest</span>();</span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="property">onreadystatechange</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">           <span class="keyword">if</span> (xmlhttp.<span class="property">status</span> == <span class="number">200</span>) &#123;</span><br><span class="line">               <span class="keyword">if</span> (xmlhttp.<span class="property">responseText</span> == <span class="string">&quot;1&quot;</span>) &#123;</span><br><span class="line"></span><br><span class="line">                   <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;sxh&quot;</span>).<span class="property">innerHTML</span> = <span class="string">&quot;开启&quot;</span></span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line"></span><br><span class="line">                   <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;sxh&quot;</span>).<span class="property">innerHTML</span> = <span class="string">&quot;关闭&quot;</span></span><br><span class="line">                   <span class="comment">//  document.getElementById(&quot;sxh&quot;).innerHTML = xmlhttp.responseText;</span></span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">          </span><br><span class="line">       &#125;</span><br><span class="line">       xmlhttp.<span class="title function_">open</span>(<span class="string">&quot;GET&quot;</span>, <span class="string">&quot;/sjhq?sjhq=&quot;</span> + <span class="string">&quot;sxh&quot;</span>);</span><br><span class="line">       xmlhttp.<span class="title function_">send</span>();</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<hr>
<h1 id="项目进度"><a href="#项目进度" class="headerlink" title="项目进度"></a>项目进度</h1><ol>
<li>2022年3月17日<br> 主要完成了18B20温度传感器软硬件的实现，并调试成功。使用了文件系统并上传网页到ESP8266闪存里、Web  服务器</li>
<li>2022年3月18日<br> 今天上课上到下午4点，又排队做核酸检测，头疼。什么都没做</li>
<li>2022年3月19日<br>   今天实现了WIFI配网的移植，主要有Ajax技术，但存储WIFI功能还未实现</li>
<li>2022年3月20日<br>今天实现了WIFI配网成功后把WIFI SSID、Password存储到EEPROM中，重启后自动连接此WIFI。</li>
<li>2022年3月21日<br> 今天上了一天课，啥都没做。</li>
<li>2022年3月22日<br>今天上课到下午6点，晚上把index.html主页内容显示到8266上，并成功显示了温度数据，每3秒更新一次。但8266连接热点后不能域名解析了。</li>
<li>2022年3月23日<br> 今天没做</li>
<li>2022年3月24日<br> 今天没做</li>
<li>2022年3月25日<br>今天没做</li>
<li>2022年3月26日<br>今天上午刚收到快递，快递等的实在是太久了！今天把外围设备都焊接调试好了，只逻辑代码了，还需要解决默认进入后台页面地址问题，明天准备接一个OLED屏幕，把每次的ip地址显示出来，直接使用地址来访问页面。</li>
<li>2022年3月27日<br>今天上午实现了OLED屏幕显示，最开始使用u8glib库，但有bug。后来换成另外一个库成功跑通。下午、晚上实现了OLED上显示系统ip、系统提示基本信息。</li>
<li>2022年3月28日<br>今天总体测试，排除可能存在的bug、优化使用体验。</li>
<li>2022年3月29日<br>今天没做</li>
<li>2022年3月30日<br>今天没做</li>
<li>2022年3月31日<br>晚上最后测试。打包快递。</li>
</ol>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/" data-id="clhnlozw700037ku8ag4f31yw" data-title="物联网毕设-智能鱼缸" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
</article>



  
    <article id="post-硬件项目汇总" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/" class="article-date">
  <time class="dt-published" datetime="2022-09-25T07:41:33.000Z" itemprop="datePublished">2022-09-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="p-name article-title" href="/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/">硬件项目汇总</a>
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>&#x3D;&#x3D; 硬件项目汇总: &#x3D;&#x3D;<br><a target="_blank" rel="noopener" href="https://oshwhub.com/xinhard/hong-wai-ce-wen-qiangstm32f1">STM32红外测温枪</a><br><a target="_blank" rel="noopener" href="https://oshwhub.com/xinhard/dian-tao-lu-kai-fa">电陶炉控制板</a><br><a target="_blank" rel="noopener" href="https://oshwhub.com/xinhard/stm32-wu-lian-wangwifi-cha-zuo">小爱同学灯</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/" data-id="clhnlozw600027ku8foe82hgb" data-title="硬件项目汇总" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
</article>



  


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2023/05/">May 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2023/01/">January 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/xinhard/archives/2022/09/">September 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/xinhard/2023/05/14/STM32%E5%8F%AF%E8%B0%83%E7%94%B5%E6%BA%90/">STM32可调电源</a>
          </li>
        
          <li>
            <a href="/xinhard/2023/01/28/IIC%E5%8D%8F%E8%AE%AE%E5%AD%A6%E4%B9%A0/">IIC协议学习</a>
          </li>
        
          <li>
            <a href="/xinhard/2022/09/25/%E7%89%A9%E8%81%94%E7%BD%91%E6%AF%95%E8%AE%BE-%E6%99%BA%E8%83%BD%E9%B1%BC%E7%BC%B8/">物联网毕设-智能鱼缸</a>
          </li>
        
          <li>
            <a href="/xinhard/2022/09/25/%E7%A1%AC%E4%BB%B6%E9%A1%B9%E7%9B%AE%E6%B1%87%E6%80%BB/">硬件项目汇总</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 lxp<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/xinhard/" class="mobile-nav-link">Home</a>
  
    <a href="/xinhard/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/xinhard/js/jquery-3.4.1.min.js"></script>



  
<script src="/xinhard/fancybox/jquery.fancybox.min.js"></script>




<script src="/xinhard/js/script.js"></script>





  </div>
</body>
</html>